import store from '../vuex/store'
import { MessageBox ,Message} from 'element-ui'
import {get,post} from '../service/axiosConfig'
import reHttp from '../service/http'
/**
 * 检测角色是否有某个操作权限
 * @param APPID
 * @param 
 * @param 
 * @returns {boolean}
 */

export function havePower(APPID) {
    const powerData = store.state.login.authority.data;//[obj,obj..]
    const havePower = powerData.some((item,index)=>{
        if (item.APPID == APPID) {
            return true
        }
    });
    return havePower;
}

/**
 * 检测角色当前的权限列表
 * @param APPID
 * @param 
 * @param 
 * @returns {boolean}
 */

export function powerList() {
    const powerData = store.state.login.authority.data;//[obj,obj..]
    const powerLists = powerData.map((item,index)=>{
        return item.MC
    });
    // console.log(powerLists)
    return powerLists;
}

/**
 * 时间格式化
 */
export function getNowFormatDate(date) {
    console.log(date)
    var seperator1 = "-";
    var seperator2 = ":";
    var month = date.getMonth() + 1;
    var strDate = date.getDate();
    var hours = date.getHours();
  
    var minutes = date.getMinutes();
    var secondes=date.getSeconds();
    if (month >= 1 && month <= 9) {
        month = "0" + month;
    }
    if (strDate >= 0 && strDate <= 9) {
        strDate = "0" + strDate;
    }
    if(hours>=0 && hours <=9){
        hours = "0" +hours;
    }
    if(minutes>=0 && minutes <=9){
        minutes = "0" +minutes;
    }
    if(secondes>=0 && secondes <=9){
        secondes = "0" +secondes;
    }
    var currentdate = date.getFullYear() + seperator1 + month + seperator1 + strDate
        + " " + hours + seperator2 + minutes
        + seperator2 + secondes;
    return currentdate;
}

/**
 * 消息提示框
 */
export function openMessgeTip (message,title) {
    MessageBox.alert(message, title, {
        confirmButtonText: '确定',
        callback: action => {

        }
    });
}

/**
 * 确认消息
 */
export function confirmFun (message, type, callback) {
    MessageBox.confirm(message, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: type,
    }).then(() => {
        callback();
    }).catch(() => {

    });
}

/**
 *无需确认消息提示框 1秒后自动关闭
 */
export function messageHints (message,type) {
    Message({
        message: message,
        type: type,
        duration:1000
    });
}

export function eachTreeData(data,name,value,childName){
    var result=[];
    if(data){
        $.each(data,(index,item) =>{
            if(item[name] == value){
                result.push(item);
            }
            else if(item.child){
              result.concat(eachTreeData(item[childName],name,value,childName));
            }
        })
    }
    return result;
}

export default {
    havePower,
    powerList,
    getNowFormatDate,
    openMessgeTip,
    messageHints,
    confirmFun,
    eachTreeData
}